package sort;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;
import java.util.jar.Attributes;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-04-14
 * Time：14:23
 */
public class TestMySort {


    /**
     * 初始化数组元素
     * @param arr 把数组传入进行赋值
     */


    // 有序数组
    public static void order(int []arr) {

        for (int i = 0; i < arr.length; i++) {
            arr[i]=i;
        }
    }


    // 无序数组
    public static void noOrder(int []arr) {

        Random r=new Random();

        for (int i = 0; i < arr.length; i++) {
            arr[i]= r.nextInt(10_0000);
        }
    }


    // 倒序数组
    public static void antiOrder(int []arr) {

        int sz= arr.length;
        for (int i = 0; i < sz; i++) {
            arr[i]=sz-i;
        }

    }


    /**
     * 测试每一种排序所耗费的时间
     * @param array
     */


    public static void testBubbleSort(int[]array) {
        array=Arrays.copyOf(array,array.length);

        long prev=System.currentTimeMillis();

        MySort.bubbleSort(array);
        long cur=System.currentTimeMillis();
        System.out.println("冒泡排序所耗时间:"+(cur-prev));
    }



    public static void testFastSort(int[]array) {
        array=Arrays.copyOf(array,array.length);

        long prev=System.currentTimeMillis();

        MySort.fastSort(array);

        long cur=System.currentTimeMillis();
        System.out.println("快速排序所耗时间:"+(cur-prev));
    }


    public static void testHeapSort(int[]array) {
        array=Arrays.copyOf(array,array.length);

        long prev=System.currentTimeMillis();

        MySort.heapSort(array);
        long cur=System.currentTimeMillis();
        System.out.println("堆排序所耗时间:"+(cur-prev));
    }

    /**
     * prev: 调用排序前的的时间搓
     * cur: 调用排序后的时间搓
     * @param array
     */

    public static void testInsertSort(int[]array) {
        array=Arrays.copyOf(array,array.length);

        // 调用排序前的的时间搓
        long prev=System.currentTimeMillis();

        MySort.insertSort(array);

        // 调用排序后的时间搓
        long cur=System.currentTimeMillis();

        // 差值就是所耗时间
        System.out.println("插入排序所耗时间:"+(cur-prev));
    }

    public static void testShellSort(int[]array) {
        array=Arrays.copyOf(array,array.length);

        long prev=System.currentTimeMillis();

        MySort.shellSort(array);
        long cur=System.currentTimeMillis();
        System.out.println("希尔排序所耗时间:"+(cur-prev));
    }

    public static void testSelectSort(int[]array) {
        array=Arrays.copyOf(array,array.length);

        long prev=System.currentTimeMillis();

        MySort.selectSort(array);
        long cur=System.currentTimeMillis();
        System.out.println("选择排序所耗时间:"+(cur-prev));
    }

    public static void testMergeSort(int[]array) {
        array=Arrays.copyOf(array,array.length);

        long prev=System.currentTimeMillis();

        MySort.mergeSort(array);
        long cur=System.currentTimeMillis();
        System.out.println("归并排序所耗时间:"+(cur-prev));
    }

    public static void testCountSort(int[]array) {
        array=Arrays.copyOf(array,array.length);

        long prev=System.currentTimeMillis();

        MySort.countSort(array);
        long cur=System.currentTimeMillis();
        System.out.println("计数排序所耗时间:"+(cur-prev));
    }

    public static void testRadixSort(int[]array) {
        array=Arrays.copyOf(array,array.length);

        long prev=System.currentTimeMillis();

        MySort.radixSort(array);
        long cur=System.currentTimeMillis();
        System.out.println("基数排序所耗时间:"+(cur-prev));
    }


    public static void testBucketSort(int[]array) {
        array=Arrays.copyOf(array,array.length);

        long prev=System.currentTimeMillis();

        MySort.bucketSort(array);
        long cur=System.currentTimeMillis();
        System.out.println("桶排序所耗时间:"+(cur-prev));
    }
    public static void main1(String[] args) {
       int[]array=new int[10_0000];

       noOrder(array);
       // 有序

       testHeapSort(array);
       // 堆排序

        testShellSort(array);
       // 希尔排序

        testFastSort(array);
        // 快速排序

        testInsertSort(array);
//        // 插入排序
//
        testBubbleSort(array);
//        // 冒泡排序
//
        testSelectSort(array);
//        // 选择排序

        testMergeSort(array);
        // 归并排序

        testCountSort(array);
        // 计数排序

        testRadixSort(array);
        // 基数排序

        testBucketSort(array);
        // 桶排序
    }



    public static void main(String[] args) {

        int[]array={3,8,1,9,6,10,5,2,7,4};

        System.out.println("==========排序前========");
        System.out.println(Arrays.toString(array));

        int[]array1= Arrays.copyOf(array,array.length);
        System.out.println("========插入排序=========");
        sort.MySort.insertSort(array1);
        System.out.println(Arrays.toString(array1));


        System.out.println("=========希尔排序========");
        int[]array2= Arrays.copyOf(array,array.length);
        MySort.shellSort(array2);
        System.out.println(Arrays.toString(array2));


        System.out.println("=======选择排序========");
        int[]array3= Arrays.copyOf(array,array.length);
        MySort.selectSort2(array3);
        System.out.println(Arrays.toString(array3));

        System.out.println("=========堆排序=========");
        int[]array4= Arrays.copyOf(array,array.length);
        MySort.heapSort(array4);
        System.out.println(Arrays.toString(array4));

        System.out.println("========== 冒泡排序 ==========");
        int[]array5= Arrays.copyOf(array,array.length);
        MySort.bubbleSort(array5);
        System.out.println(Arrays.toString(array5));


        System.out.println("========== 快速排序 ==========");
        int[]array6= {3,8,6,7,2,3,4,5};
        MySort.fastSort(array6);
        System.out.println(Arrays.toString(array6));


        System.out.println("========== 归并排序 ==========");
        int[]array7={102,108,112,106,117,109,109,108,100,119,200,98,22};
        MySort.mergeSort(array7);
        System.out.println(Arrays.toString(array7));

        System.out.println("========= 计数排序 ========");
       int[]array8={102,108,112,106,117,109,109,108};
       MySort.countSort(array8);
        System.out.println(Arrays.toString(array8));

        System.out.println("========= 基数排序 =========");
        int []array9={98,135,156,258,335,658,745,946};
        MySort.radixSort(array9);
        System.out.println(Arrays.toString(array9));


        System.out.println("=========== 桶排序 ==========");
        int[]array10={11,24,56,15,99,42,86,25,12,27,87,100,1988,156782,12313};
        MySort.bucketSort(array10);
        System.out.println(Arrays.toString(array10));


    }
}
